using UnityEngine;

//敌人波数
[System.Serializable]
public class EnemySpawnSequence
{
    //通过不同工厂生产不同敌人类型
    [SerializeField]
    EnemyFactory factory = default;

    //敌人种类
    [SerializeField]
    EnemyType type = EnemyType.Medium;

    //敌人数量
    [SerializeField, Range(1,100)]
    int amount = 1;

    //生成速度
    [SerializeField, Range(0.1f, 10f)]
    float cooldown = 1f;

    

    
    public State Begin() => new State(this);


    //使用结构而不是类来避免内存分配
    [System.Serializable]
    public struct State
    {
        EnemySpawnSequence sequence;

        //产生敌人的数量
        int count;

        //冷却进度
        float cooldown;
        
        public State (EnemySpawnSequence sequence)
        {
            this.sequence = sequence;
            count = 0;
            cooldown = sequence.cooldown;
        }

        public float Progress(float deltaTime)
        {
            //设置冷却时间
            cooldown += deltaTime;
            while(cooldown >= sequence.cooldown)
            {
                cooldown -= sequence.cooldown;
                if(count >= sequence.amount)
                    return cooldown;//如果当前序列完成时，冷却时间超出，我们返回额外的时间用于下一序列
                count+= 1;
                //调用怪物生成
                Game.SpawnEnemy(sequence.factory, sequence.type);
            }
            return -1f;
        }
    }
}